﻿package derek.geom
{
	public class vector2D
	{

		public var x:Number;
		public var y:Number;
		
		public function vector2D(tx:Number,ty:Number)
		{
			if (tx) {x=tx;} else {x=0;}
			if (ty) {y=ty;} else {y=0;}
		}
	
		public function clone():vector2D
		{
			return new vector2D(x,y);
		}
	
		public function addVector(vector:vector2D):void
		{
			x+=vector.x;
			y+=vector.y;
		}
	
		public function mult(factor:Number):void
		{
			x*=factor;
			y*=factor;
		}
	
		public function normalise():void
		{
			var tLength:Number=length;
			x/=tLength;
			y/=tLength;
		}
		public function get length():Number
		{
			return Math.sqrt(x*x+y*y);
		}
	
		public function distanceTo(tVector:vector2D):Number
		{
			var dx:Number=tVector.x-x;
			var dy:Number=tVector.y-y;
			return Math.sqrt(dx*dx+dy*dy);
		}
	
		public function dot(tVector:vector2D ):Number
		{
			// Clone
			var v1:vector2D=this.clone();
			var v2:vector2D=tVector.clone();
		
			// Normalise
			v1.normalise();
			v2.normalise();
		
			return v1.x*v2.x+v1.y*v2.y;
		}
	
		public function rotate(tAngle:Number):void {
			var newx:Number = Math.cos(tAngle) * x - Math.sin(tAngle) * y;
			var newy:Number = Math.cos(tAngle) * y + Math.sin(tAngle) * x;
			x=newx
			y=newy;
		}
	
		public function rotateAboutPoint(tPoint:vector2D,tAngle:Number):void
		{
			// Move to origin
			x-=tPoint.x;
			y-=tPoint.y;
		
			rotate(tAngle);
		
			// Move back to area around point
			x+=tPoint.x;
			y+=tPoint.y;
		}
	}
}